package com.behome.netty.common.util;

import java.lang.reflect.*;
import java.util.*;

public class CmdKit {
    public CmdKit() {
    }

    public static <T> Map<Byte, Class<T>> mapCmdTypeIntoCmdClass(Class<T> baseClazz, Class cmdTypeAnnotationClazz, String annotationKey, String methodNameOfAnnotationValue, String... pkgNames) {
        Map<Byte, Class<T>> tmpMap = new HashMap<>();
        if(baseClazz != null && cmdTypeAnnotationClazz != null && annotationKey != null && methodNameOfAnnotationValue != null) {
            Set<Class<T>> classSet = ClassKit.getAllClassFromPkg(baseClazz, pkgNames);
            if(classSet != null) {
                Iterator<Class<T>> var7 = classSet.iterator();
                while(true) {
                    Class clazz;
                    do {
                        if(!var7.hasNext()) {
                            return Collections.unmodifiableMap(tmpMap);
                        }

                        clazz = var7.next();
                    } while(!clazz.isAnnotationPresent(cmdTypeAnnotationClazz));

                    try {
                        Object annotationEntity = clazz.getAnnotation(cmdTypeAnnotationClazz);
                        if(annotationEntity != null) {
                            InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotationEntity);
                            Field value = invocationHandler.getClass().getDeclaredField("memberValues");
                            value.setAccessible(true);
                            Map<String, Object> memberValues = (Map)value.get(invocationHandler);
                            Object cmdTypeEntity = memberValues.get(annotationKey);
                            if(cmdTypeEntity != null) {
                                Method[] methods = cmdTypeEntity.getClass().getDeclaredMethods();
                                Method[] var15 = methods;
                                int var16 = methods.length;

                                for(int var17 = 0; var17 < var16; ++var17) {
                                    Method method = var15[var17];
                                    String methodName = method.getName();
                                    if(methodNameOfAnnotationValue.equals(methodName)) {
                                        Object vaule = method.invoke(cmdTypeEntity);
                                        if(vaule instanceof Byte) {
                                            tmpMap.put((Byte)vaule, clazz);
                                        }
                                    }
                                }
                            }
                        }
                    } catch (SecurityException | NoSuchFieldException var21) {
                        var21.printStackTrace();
                    } catch (IllegalAccessException var22) {
                        var22.printStackTrace();
                    } catch (InvocationTargetException var23) {
                        var23.printStackTrace();
                    }
                }
            }
        }

        return Collections.unmodifiableMap(tmpMap);
    }
}
